package com.shd.edanpei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Pair;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.ui.adapter.DynamicsAdapter;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.bean.MessageModel;
import com.shd.edanpei.presenter.contract.DynamicsContract;
import com.shd.edanpei.presenter.DynamicsPresenter;
import com.shd.edanpei.view.RefreshLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/19.
 * 最新动态
 */

public class DynamicsActivity extends BaseActivity<DynamicsPresenter> implements DynamicsContract.DynamicsIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.refresh_layout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private DynamicsAdapter mAdapter;

    @Override
    protected int setLayout() {
        return R.layout.activity_dynamics;
    }

    @Override
    protected DynamicsPresenter bindPresenter() {
        return new DynamicsPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        headline.setText("E资讯");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new DynamicsAdapter(this);
        recyclerView.setAdapter(mAdapter);

        refreshLayout.setRefreshing(true);
        mPresenter.refresh();
    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshLayout.setRefreshing(false);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadMore();
            }
        }, recyclerView);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MessageModel item = mAdapter.getItem(position);
                Bundle bundle = new Bundle();
                bundle.putParcelable(Constant.DATA, mAdapter.getItem(position));
//                startActivity(DynamicsDetailsActivity.class, bundle);

                Intent intent = new Intent(DynamicsActivity.this, DynamicsDetailsActivity.class);
                intent.putExtras(bundle);
                ActivityOptionsCompat compat = ActivityOptionsCompat.makeSceneTransitionAnimation(DynamicsActivity.this, mAdapter.getViewByPosition(position, R.id.picture), "picture");
                ActivityCompat.startActivity(DynamicsActivity.this, intent, compat.toBundle());
            }
        });
    }

    @OnClick(R.id.iv_headline_back)
    void close() {
        finish();
    }

    @Override
    public RefreshLayout getRefreshLayout() {
        return refreshLayout;
    }

    @Override
    public DynamicsAdapter getAdapter() {
        return mAdapter;
    }
}
